﻿using System.Configuration;
using System.Text.RegularExpressions;
using RuTracker.Interface.Editors;
using RuTrackerLib.ServiceAttibutes;

namespace RuTrackerLib.Editors
{
    [PerApplicationService]
    public class BaseTagEditor : IHtmlEditor
    {
        private const string Pattern = "\\<base\\s*href=\".*?\"\\>";
        private readonly string _baseTagReplacement;

        public BaseTagEditor()
        {
            string appRootPath = ConfigurationManager.AppSettings["SiteRootPath"];
            _baseTagReplacement = string.Format("<base href=\"{0}\">", appRootPath);
        }

        public string Edit(string html)
        {
            Regex regex = new Regex(Pattern);
            return regex.Replace(html, _baseTagReplacement);
        }
    }
}
